import Vue from 'vue'
// 导入
import VueRouter from 'vue-router'
import { getItem } from '@/utils/auth.js'
// import Login from '@/views/login'
// import Home from '@/views/home/index.vue'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// 注册
Vue.use(VueRouter)
// 实例化
// const routes = [
//   {
//     path: '/login',
//     name: 'login',
//     component: Login
//   }
// ]
// 实例化
const router = new VueRouter({
  // routes
  routes: [
    {
      path: '/login',
      meta: {
        title: '极客园-登录页面'
      },
      component: () => import('@/views/login')
    },
    {
      path: '/',
      component: () => import('@/views/layout'),
      children: [
        {
          path: '/home',
          alias: '/',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页-数据概况'
          }
        },
        {
          path: '/article',
          component: () => import('@/views/article'),
          meta: {
            title: '首页-文章发表'
          }
        },
        {
          path: '/publish',
          component: () => import('@/views/publish'),
          meta: {
            title: '首页-内容'
          }
        }
      ]
    }
  ]
})
// 前置导航守卫
router.beforeEach((to, form, next) => {
  document.title = to.meta.title
  // console.log(to)
  /*
  如果有token
    如果去的是登录页
      跳转到首页
    如果不是去登录页
      正常跳转
  没有token
    如果去的是登录页
      正常跳转
    不是登录页
      跳到登录页 */
  Nprogress.start()
  if (getItem()) {
    if (to.path === '/login') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
  // next()
  Nprogress.done()
})
router.afterEach((to, form) => {
  Nprogress.done()
  // console.log(to, form)
})
// export default暴露
export default router
